const vxLogin = function(callback){
	let _this = this
	uni.getProvider({
		service: 'oauth',
		success: function(res) {
			// 手机app支持微信、qq和微博等
			console.log('res----->',res);
			if (~res.provider.indexOf('weixin')) {
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(JSON.stringify(loginRes));
						// 获取用户信息
						uni.getUserInfo({
							provider: 'weixin',
							success: async function(infoRes) {
								let query = {   
									cmd: "mMember_AppLogin",
									para: { ltype: "unionid", keyid: infoRes.userInfo.unionId }
								}
								console.log('query---->',query);
								let result = await uni.$u.get('/ajax/app.ashx',query)
								uni.showToast({
								    title: result,
								    duration: 2000
								});
								console.log('result----->',result)
								if(result.Data.username){
									if(JSON.stringify(result.Data) !== '{}' && result.Data.ak){
										callback(result)
									}
								}else{
									uni.navigateTo({    
										url:"/pages/myuser/user/bind?wxUserInfo=" + JSON.stringify(infoRes.userInfo)  
									})
								}
								const userInfo = infoRes.userInfo
								getApp().globalData.userInfo = userInfo;
								getApp().globalData.wxCode = userInfo.nickName;	
							}	
						});
					},
					fail: function(res) {
						console.log(res)
						uni.showToast({
							icon: 'none',
							title: '登陆失败,请确保已安装或已登录微信',
							//title: res.errMsg,
							duration: 2000
						});
					}
				});
			}
		}
	});

}
const qqLogin = function(callback){
	let _this = this
	uni.getProvider({
		service: 'oauth',
		success: function(res) {
			// 手机app支持微信、qq和微博等
			if (~res.provider.indexOf('qq')) {
				console.log('qq');
				uni.login({
					provider: 'qq',
					success: async function(loginRes) {
						console.log(JSON.stringify(loginRes.authResult));
						// 获取用户信息
						uni.getUserInfo({
							provider: 'qq',
							success: async function(infoRes) {
								console.log('QQinfoRes----->',infoRes);
								var qqid = infoRes.userInfo.unionid
								let query = {
									cmd: "mMember_AppLogin",
									para: { ltype: "qqid", keyid: qqid }
								}
								let result = await uni.$u.get('/ajax/app.ashx',query)
								if(result.Data !== "{}" && result.Data.ak){
									callback(result)
								}else{
									let userInfo_s = {}
									userInfo_s.qqid = qqid
									userInfo_s.nickname = infoRes.userInfo.nickname
									userInfo_s.figureurl_2 = infoRes.userInfo.figureurl_2
									uni.navigateTo({
										url:"/pages/myuser/user/bind?qqUserInfo=" + JSON.stringify(userInfo_s)  
									})
								}
								const userInfo = infoRes.userInfo
								getApp().globalData.userInfo = userInfo;
								getApp().globalData.wxCode = userInfo.nickName;
								console.log(getApp().globalData.userInfo);
							}
						});
					},
					fail: function(res) {
						console.log(res)
						uni.showToast({
							icon: 'none',
							title: '登陆失败,请确保已安装或已登QQ',
							duration: 2000
						});
					}
				});
			}
		}
	});
				
}
module.exports = {
	vxLogin,
	qqLogin
}